<?php

namespace App\Repositories\Impl;

use App\Models\Live;
use App\Repositories\Contracts\LiveRepository;
use App\Repositories\Transformers\LiveTransformer;

class LiveRepositoryImpl implements LiveRepository
{
    public function paginate($perPage, $filter)
    {
        if (!empty($filter['keyword'])) {
            $live = Live::where('title', 'like', '%' . $filter['keyword'] . '%')
                ->paginate($perPage);
        } else {
            $live = Live::paginate($perPage);
        }

        $result = fractal($live, new LiveTransformer());

        return $result->toArray();
    }

    public function find($id, $columns = ['*'])
    {
        return Live::find($id, $columns);
    }

    public function findByFilter($filter, $columns = ['*'])
    {
        return Live::where($filter)->first($columns);
    }

    public function store($data)
    {
        $live = Live::create($data);
        $result = fractal($live, new LiveTransformer());
        return $result->toArray();

    }

    public function update($data, $id)
    {
        return Live::whereId($id)->updateOrCreate($data);
    }

    public function updateByFilter($data, $where)
    {
        return Live::where($where)->update($data);
    }

    public function delete($id)
    {
        return Live::whereId($id)->delete();
    }
}
